package com.zcsy.manage.owner.controller.pay;


import java.io.StringReader;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.InputSource;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.util.Globals;
import com.zcsy.commons.util.PropertiesUtil;
import com.zcsy.commons.util.WeChatUtils;
import com.zcsy.manage.model.mall.OrderDetailInfo;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.model.orders.AlipayCerts;
import com.zcsy.manage.model.orders.MobileBuildOrder;
import com.zcsy.manage.model.pay.alipay.AliPayResult;
import com.zcsy.manage.model.property.CarBillDetail;
import com.zcsy.manage.model.property.FeeBillDetail;
import com.zcsy.manage.model.wxpay.WxPayReturnData;
import com.zcsy.manage.model.wxpay.WxPaySendData;
import com.zcsy.manage.model.wxpay.WxPayToken;
import com.zcsy.manage.owner.controller.pay.alipay.config.AlipayConfig;
import com.zcsy.manage.owner.controller.pay.alipay.util.AlipayNotify;
import com.zcsy.manage.owner.controller.pay.util.PayConstants;
import com.zcsy.manage.service.alipay.service.IAlipayService;
import com.zcsy.manage.service.mall.IOrderDetailInfoService;
import com.zcsy.manage.service.mall.IOrderInfoService;
import com.zcsy.manage.service.property.ICarBillDetailService;
import com.zcsy.manage.service.property.IFeeBillDetailService;
import com.zcsy.manage.service.wxpay.service.IAppWxPayService;
import com.zcsy.manage.service.wxpay.service.WxPayResult;


@Controller
@RequestMapping(value = "pay")
public class PayController {
	
	//微信返回标识
    public static final String RETURN_CODE_SUCCESS = "SUCCESS";
    public static final String RETURN_CODE_FAILD = "FAIL";
    
    public static final int SHOPPING = 1;//商城购物
	public static final int FEE_BILL = 2;//物业费
	public static final int CAR_BILL = 3;//停车费
    
    private static final int WX_APPEND = 3;//微信支付订单号加后缀

    private static Logger log = LoggerFactory.getLogger(PayController.class);
    
    @Autowired
    private IAlipayService service;

    @Autowired
    private IOrderInfoService orderService;

    @Autowired
    private IAppWxPayService wxPayService;
    
    @Autowired
    private IFeeBillDetailService feeBillDetailService;
    
    @Autowired
    private ICarBillDetailService carBillDetailService;
    
    @Autowired
	private IOrderDetailInfoService orderDetailInfoService;

    /** 
     * @Title: getAliPayParams 
     * @Description: 获取支付宝回调参数
     * @param @param request
     * @param @return     
     * @return Map<String,String>     
     * @throws 
     */
    private AliPayResult getAliPayParams(HttpServletRequest request){
    	Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String)iter.next();
            String[] values = (String[])requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++ ) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr
                                                                             + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }

        log.debug("！！！！！！！！支付宝返回参数：" + params.toString());
        if (AlipayNotify.verify(params)){
        	AliPayResult alipay = new AliPayResult();
            // 商户订单号
            alipay.setExternal_trade_no(new String(request.getParameter("out_trade_no")));

            // 交易状态
            alipay.setExternal_resp_code(new String(request.getParameter("trade_status")));
            
            // 交易状态
            alipay.setTotal_fee(new BigDecimal(new String(request.getParameter("total_fee"))));
            return alipay;
        }
        
        return null;
    }
    
    /**
     * @Title: notifyUrl
     * @Description: 支付宝调用的异步通知
     * @param @param out_trade_no 商户订单号
     * @param @param trade_no 支付宝交易号
     * @param @param trade_status 交易状态
     * @param @param request
     * @param @return
     * @return String
     * @throws
     */
    @RequestMapping(value = "alipaynotify", method = RequestMethod.POST)
    @ResponseBody
    public String alipaynotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！订单异步支付调用!！！！！！！！！");
        String result = "fail";
        try {
            // 异步通知
            // 获取支付宝POST过来反馈信息
        	AliPayResult alipay = getAliPayParams(request);
            
            if (alipay != null) {// 验证成功
            	alipay.setPaySystem(Constants.PAY_SYSTEM_APP);
                if (alipay.getExternal_resp_code().equals("TRADE_FINISHED")) {
                    // 退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                } else if (alipay.getExternal_resp_code().equals("TRADE_SUCCESS")) {
                    // 付款完成后，支付宝系统发送该交易状态通知
                    orderService.payOrder(alipay.getExternal_trade_no(), alipay);
                }

                result = "success";
                log.debug("！！！！！！！！！！！！！！！！！！！！订单异步支付完毕!！！！！！！！！");
                // ////////////////////////////////////////////////////////////////////////////////////////
            } else {// 验证失败
                result = "fail";
                log.debug("！！！！！！！！！！！！！！！！！！！！订单异步支付校验失败!！！！！！！！！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    
    @RequestMapping(value = "alipaycarnotify", method = RequestMethod.POST)
    @ResponseBody
    public String alipaycarnotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！停车费异步支付调用!！！！！！！！！");
        String result = "fail";
        try {
        	// 异步通知
            // 获取支付宝POST过来反馈信息
        	AliPayResult alipay = getAliPayParams(request);
            
            if (alipay != null) {// 验证成功

                if (alipay.getExternal_resp_code().equals("TRADE_FINISHED")) {
                    // 退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                } else if (alipay.getExternal_resp_code().equals("TRADE_SUCCESS")) {
                    // 付款完成后，支付宝系统发送该交易状态通知
                	carBillDetailService.payCarBill(alipay.getExternal_trade_no(), alipay);
                }

                result = "success";
                log.debug("！！！！！！！！！！！！！！！！！！！！停车费异步支付完毕!！！！！！！！！");
                // ////////////////////////////////////////////////////////////////////////////////////////
            } else {// 验证失败
                result = "fail";
                log.debug("！！！！！！！！！！！！！！！！！！！！停车费异步支付校验失败!！！！！！！！！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    
    @RequestMapping(value = "alipaypropertynotify", method = RequestMethod.POST)
    @ResponseBody
    public String alipaypropertynotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！物业费异步支付调用!！！！！！！！！");
        String result = "fail";
        try {
        	// 异步通知
            // 获取支付宝POST过来反馈信息
        	AliPayResult alipay = getAliPayParams(request);
            
            if (alipay != null) {// 验证成功

                if (alipay.getExternal_resp_code().equals("TRADE_FINISHED")) {
                    // 退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                } else if (alipay.getExternal_resp_code().equals("TRADE_SUCCESS")) {
                    // 付款完成后，支付宝系统发送该交易状态通知
                	feeBillDetailService.payFeeBill(alipay.getExternal_trade_no(), alipay);
                }

                result = "success";
                log.debug("！！！！！！！！！！！！！！！！！！！！物业费异步支付完毕!！！！！！！！！");
                // ////////////////////////////////////////////////////////////////////////////////////////
            } else {// 验证失败
                result = "fail";
                log.debug("！！！！！！！！！！！！！！！！！！！！物业费异步支付校验失败!！！！！！！！！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    
    
    /** 
     * @Title: getWxPayParams 
     * @Description: 获取微信支付的回调结果
     * @param @param request
     * @param @return     
     * @return WxPayResult     
     * @throws 
     */
    private WxPayResult getWxPayParams(HttpServletRequest request){
    	String inputLine;
        String notityXml = "";

        try {
            while ((inputLine = request.getReader().readLine()) != null) {
                notityXml += inputLine;
            }
            request.getReader().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.debug("接收到的报文：" + notityXml);

        Map m = parseXmlToList2(notityXml);
        WxPayResult wpr = new WxPayResult();
        wpr.setAppid(m.get("appid").toString());
        wpr.setBankType(m.get("bank_type").toString());
        wpr.setCashFee(m.get("cash_fee").toString());
        wpr.setFeeType(m.get("fee_type").toString());
        wpr.setIsSubscribe(m.get("is_subscribe").toString());
        wpr.setMchId(m.get("mch_id").toString());
        wpr.setNonceStr(m.get("nonce_str").toString());
        wpr.setOpenid(m.get("openid").toString());
        wpr.setOutTradeNo(m.get("out_trade_no").toString());
        wpr.setResultCode(m.get("result_code").toString());
        wpr.setReturnCode(m.get("return_code").toString());
        wpr.setSign(m.get("sign").toString());
        wpr.setTimeEnd(m.get("time_end").toString());
        wpr.setTotalFee(m.get("total_fee").toString());
        wpr.setTradeType(m.get("trade_type").toString());
        wpr.setTransactionId(m.get("transaction_id").toString());
        
        return wpr;
    }
    
    /**
     * @Title: wxnotify
     * @Description: 微信调用的异步通知
     * @param @param request
     * @param @return
     * @return String
     * @throws
     */
    @RequestMapping(value = "wxnotify", method = RequestMethod.POST)
    @ResponseBody
    public String wxnotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！微信支付回调数据开始!！！！！！！！！");
        String result = "fail";

        WxPayResult wpr = getWxPayParams(request);
        wpr.setPaySystem(Constants.PAY_SYSTEM_APP);

        if ("SUCCESS".equals(wpr.getResultCode())) {
            // 支付成功
            // 付款完成后，微信系统发送该交易状态通知
        	
        	//微信的订单号减去随机后缀
        	String orderNo = wpr.getOutTradeNo().substring(0, wpr.getOutTradeNo().length() - WX_APPEND - 1);
        	orderService.payOrder(orderNo, wpr);

            log.debug("！！！！！！！！！！！！！！！！！！！！微信支付回调数据成功!！！！！！！！！");
            result = "success";
        } else {
            result = "fail";
        }
        return result;
    }
    
    @RequestMapping(value = "wxpropertynotify", method = RequestMethod.POST)
    @ResponseBody
    public String wxpropertynotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！微信支付物业费回调数据开始!！！！！！！！！");
        String result = "fail";

        WxPayResult wpr = getWxPayParams(request);
        wpr.setPaySystem(Constants.PAY_SYSTEM_APP);

        if ("SUCCESS".equals(wpr.getResultCode())) {
            // 支付成功
            // 付款完成后，微信系统发送该交易状态通知
        	
        	//微信的订单号减去随机后缀
        	String orderNo = wpr.getOutTradeNo().substring(0, wpr.getOutTradeNo().length() - WX_APPEND - 1);
        	feeBillDetailService.payFeeBill(orderNo, wpr);

            log.debug("！！！！！！！！！！！！！！！！！！！！微信支付物业费回调数据成功!！！！！！！！！");
            result = "success";
        } else {
            result = "fail";
        }
        return result;
    }
    
    @RequestMapping(value = "wxcarnotify", method = RequestMethod.POST)
    @ResponseBody
    public String wxcarnotify(@Context HttpServletRequest request) {
        log.debug("！！！！！！！！！！！！！！！！！！！！微信支付停车费回调数据开始!！！！！！！！！");
        String result = "fail";

        WxPayResult wpr = getWxPayParams(request);
        wpr.setPaySystem(Constants.PAY_SYSTEM_APP);

        if ("SUCCESS".equals(wpr.getResultCode())) {
            // 支付成功
            // 付款完成后，微信系统发送该交易状态通知
        	
        	//微信的订单号减去随机后缀
        	String orderNo = wpr.getOutTradeNo().substring(0, wpr.getOutTradeNo().length() - WX_APPEND - 1);
        	carBillDetailService.payCarBill(orderNo, wpr);

            log.debug("！！！！！！！！！！！！！！！！！！！！微信支付停车费回调数据成功!！！！！！！！！");
            result = "success";
        } else {
            result = "fail";
        }
        return result;
    }

    /** 
     * @Title: buildPayOrder 
     * @Description: 支付接口，微信会生成预订单
     * @param @param request
     * @param @param orderId 商城购物传订单编号，物业停车费传账单ID
     * @param @param payType 1,支付宝;2,微信;3,贝壳
     * @param @param payFor  1,商城;2,物业费;3,停车费
     * @param @return     
     * @return RequestJson     
     * @throws 
     */
    @RequestMapping(value = "buildPayOrder")
    @ResponseBody
    public RequestJson buildPayOrder(HttpServletRequest request,
                                     String orderId,int payType , int payFor) {
        RequestJson json = new RequestJson();
        try {
        	MobileBuildOrder info = null;
        	
        	switch (payFor) {
			case SHOPPING:{//商城购物
				OrderInfo order = orderService.selectOrderByPrimaryKey(orderId);
	            if (order == null) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("订单不存在");

	                return json;
	            }

	            if (order.getState() != Constants.ORDER_UNPAID) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("该订单状态不需要支付");

	                return json;
	            }
	            
	            info = buildPay(order);
				break;
			}
			
			case FEE_BILL:{//物业费缴费
				FeeBillDetail feeBill = feeBillDetailService.selectByPrimaryKey(orderId);
	            if (feeBill == null) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("订单不存在");

	                return json;
	            }

	            if (feeBill.getState() != Constants.AFTER_STATE
	            		&&feeBill.getState() != Constants.BEFORE_STATE) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("该订单状态不需要支付");

	                return json;
	            }

	            info = buildPay(feeBill);
				break;
			}
			
			case CAR_BILL:{//停车费缴费
				CarBillDetail carBill = carBillDetailService.selectByPrimaryKey(orderId);
	            if (carBill == null) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("订单不存在");

	                return json;
	            }

	            if (carBill.getState() != Constants.AFTER_STATE
	            		&&carBill.getState() != Constants.BEFORE_STATE) {
	            	json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
	                json.setMsg("该订单状态不需要支付");

	                return json;
	            }
	            
	            info = buildPay(carBill);
				break;
			}
			default:
				break;
			}

            switch (payType) {
                case PayConstants.ALIPAY:{
                    // 支付宝
                	AlipayCerts certs = new AlipayCerts();
                    certs.setAccount(AlipayConfig.ali_account);
                    certs.setPartner(AlipayConfig.partner);
                    certs.setPrivateKey(AlipayConfig.private_key);
                    certs.setPublicKey(AlipayConfig.ali_public_key);
                    
                    StringBuffer buffer = new StringBuffer(Globals.getConfig("app_pay_notify_prefix"));
    				switch (payFor) {
    				case SHOPPING: {
    					buffer.append("/alipaynotify");
    					certs.setNotifyUrl(buffer.toString());
    					break;
    				}
    				case FEE_BILL: {
    					buffer.append("/alipaypropertynotify");
    					certs.setNotifyUrl(buffer.toString());
    					break;
    				}
    				case CAR_BILL: {
    					buffer.append("/alipaycarnotify");
    					certs.setNotifyUrl(buffer.toString());
    					break;
    				}
    				default:
    					break;
    				}
    				log.debug("！！！！！！！！！支付宝支付回调地址---》" + certs.getNotifyUrl());
                    
                    info.setCerts(certs);
                    break;
                }
                case PayConstants.WXPAY: {
                	// 微信下单
                	WxPaySendData sendData = new WxPaySendData();

    				sendData.setBody(info.getName());

    				// 支付金额,单位为分
    				sendData.setTotal_fee(info.getWxPrice());

    				sendData.setOut_trade_no(info.getOrderNo() + getWxAppend());

    				StringBuffer buffer = new StringBuffer(Globals.getConfig("app_pay_notify_prefix"));
    				switch (payFor) {
    				case SHOPPING: {
    					buffer.append("/wxnotify");
    					sendData.setNotify_url(buffer.toString());
    					break;
    				}
    				case FEE_BILL: {
    					buffer.append("/wxpropertynotify");
    					sendData.setNotify_url(buffer.toString());
    					break;
    				}
    				case CAR_BILL: {
    					buffer.append("/wxcarnotify");
    					sendData.setNotify_url(buffer.toString());
    					break;
    				}
    				default:
    					break;
    				}
    				log.debug("！！！！！！！！！微信支付回调地址---》" + sendData.getNotify_url());
    				
    				WxPayReturnData returnData = wxPayService
    						.buildAdvance(sendData);

    				// 生产支付签名
    				if (returnData.getReturn_code() != null
    						&& returnData.getReturn_code().equals(
    								RETURN_CODE_SUCCESS)) {
    					if (returnData.getResult_code() != null
    							&& returnData.getResult_code().equals(
    									RETURN_CODE_SUCCESS)) {
    						WxPayToken payToken = new WxPayToken();
    						payToken.setAppId(returnData.getAppid());
    						payToken.setTimestamp(WeChatUtils.create_timestamp());
    						payToken.setSignType("MD5");
    						payToken.setPackageStr(returnData.getPrepay_id());
    						payToken.setNonceStr(WeChatUtils.create_nonce_str());
    						payToken.setPartnerId(returnData.getMch_id());

    						// 重新生产支付签名，此处参与签名的字段有：appId, timeStamp, nonceStr,
    						// package, signType
    						SortedMap<Object, Object> signMap = new TreeMap<Object, Object>();
    						signMap.put("appid", returnData.getAppid());
    						signMap.put("timestamp", payToken.getTimestamp());
    						signMap.put("noncestr", payToken.getNonceStr());
    						signMap.put("package", "Sign=WXPay");
    						signMap.put("partnerid", returnData.getMch_id());
    						signMap.put("prepayid", returnData.getPrepay_id());
    						payToken.setPaySign(WeChatUtils.createAppSign(signMap,
    								returnData.getMch_secret()));
    						info.setPayToken(payToken);
    					} else {
    						json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
    						json.setMsg(returnData.getErr_code_des());
    						return json;
    					}
    				} else {
    					json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
    					json.setMsg(returnData.getReturn_msg());
    					return json;
    				}
                	break;
                }

			default:
				break;
			}

            json.setObj(info);
        } catch (Exception e) {
            e.printStackTrace();
            json.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
            json.setMsg("支付失败，请稍后重试或尝试其他支付方式");
        }

        return json;
    }
    
    
    
    private MobileBuildOrder buildPay(Object obj){
    	MobileBuildOrder info = new MobileBuildOrder();
    	if(obj instanceof OrderInfo){
    		OrderInfo order = (OrderInfo)obj;
            info.setOrderId(order.getId());
            info.setOrderNo(order.getOrderNumber());

            StringBuffer name = new StringBuffer("");

            List<OrderDetailInfo> details;
            
            if(order.getOrderLevel() == Constants.ORDER_STATE_FIRST){
            	details = orderDetailInfoService.getOrderDetailWhenFirst(order.getId());
			}else{
				details = orderService.getOrderDetailByOrderId(order.getId());
			}
            
            
            if (details != null && details.size() > 0) {
                name.append(details.get(0).getGoodsName());

                if (name.length() > 8) {
                    name = new StringBuffer(name.substring(0, 10));
                    name.append("等");
                } else if (details.size() > 1) {
                    name.append("等");
                }

                info.setName(name.toString());
            }

            info.setPrice(getMoney(order));
            info.setWxPrice(getWXMoney(order));
            info.setDesc(getGoodsDetail(details));
    	}else if(obj instanceof FeeBillDetail){
    		FeeBillDetail item = (FeeBillDetail)obj;
    		info.setOrderId(item.getId());
            info.setOrderNo(item.getId());

            StringBuffer name = new StringBuffer(item.getMonth() + "个月物业费");
            info.setName(name.toString());

            if (!"true".equals(PropertiesUtil.getIsCeshi())) {
            	info.setPrice(String.format("%.2f", item.getMoney()));
            	info.setWxPrice(item.getMoney().multiply(new BigDecimal(100)).intValue());
            }else{
            	info.setPrice("0.01");
            	info.setWxPrice(1);
            }
            info.setDesc(item.getStartDate() + "至" + item.getEndDate());
            
    	}else if(obj instanceof CarBillDetail){
    		CarBillDetail item = (CarBillDetail)obj;
    		info.setOrderId(item.getId());
            info.setOrderNo(item.getId());

            StringBuffer name = new StringBuffer(item.getMonth() + "个月停车费");
            info.setName(name.toString());

            if (!"true".equals(PropertiesUtil.getIsCeshi())) {
            	info.setPrice(String.format("%.2f", item.getMoney()));
            	info.setWxPrice(item.getMoney().multiply(new BigDecimal(100)).intValue());
            }else{
            	info.setPrice("0.01");
            	info.setWxPrice(1);
            }
            info.setDesc(item.getStartDate() + "至" + item.getEndDate());
    	}
    	
    	return info;
    }
    
    private String getWxAppend(){
    	String area = new String("abcdefghijklmnopqrstuvwxyz0123456789");
    	Random random = new Random();
    	StringBuffer buffer = new StringBuffer("_");
    	for(int i = 0 ; i < WX_APPEND ; i ++){
    		buffer.append(area.charAt(random.nextInt(area.length())));
    	}
    	
    	return buffer.toString();
    }
    
    private String getMoney(OrderInfo order) {
        // 测试阶段只用支付一分钱
        String totalPrice = "0.01";

        // 如果当前环境不是测试环境，买家付款时向卖家发送短信
        if (!"true".equals(PropertiesUtil.getIsCeshi())) {
            totalPrice = String.format("%.2f", order.getFinalPrice());
        }

        return totalPrice;
    }
    
    private int getWXMoney(OrderInfo order) {
        // 微信的单位是分
        // 测试阶段只用支付一分钱
        String totalPrice = "1";

        // 如果当前环境不是测试环境，买家付款时向卖家发送短信
        if (!"true".equals(PropertiesUtil.getIsCeshi())) {
            totalPrice = String.format("%.0f",
                order.getFinalPrice().multiply(new BigDecimal(100)).floatValue());
        }

        return Integer.valueOf(totalPrice);
    }

    private String getGoodsDetail(List<OrderDetailInfo> details) {
//		if (details != null && details.size() > 0) {
//			StringBuffer buffer = new StringBuffer();
//			for (int i = 0; i < details.size(); i++) {
//				buffer.append(details.get(i).getGoodsName() + "，");
//			}
//
//			if (buffer.length() > 0) {
//				return buffer.substring(0, buffer.length() - 1);
//			}
//		}

        return "D生活";
    }
    
    /**
     * description: 解析微信通知xml
     * 
     * @param xml
     * @return
     * @author ex_yangxiaoyi
     * @see
     */
    @SuppressWarnings({"unused", "rawtypes", "unchecked"})
    private static Map parseXmlToList2(String xml) {
        Map retMap = new HashMap();
        try {
            StringReader read = new StringReader(xml);
            // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
            InputSource source = new InputSource(read);
            // 创建一个新的SAXBuilder
            SAXBuilder sb = new SAXBuilder();
            // 通过输入源构造一个Document
            Document doc = (Document)sb.build(source);
            Element root = doc.getRootElement();// 指向根节点
            List<Element> es = root.getChildren();
            if (es != null && es.size() != 0) {
                for (Element element : es) {
                    retMap.put(element.getName(), element.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retMap;
    }
}
